package android.slc.app.startup.task;

import android.slc.app.startup.TaskConstant;
import android.slc.appbase.api.slc.config.ApiConfig;
import android.slc.appbase.repository.local.sp.AppConfig;
import android.slc.box.anchors.task.Task;
import android.slc.box.commonlibrary.compat.SlcResourceUtils;
import android.slc.box.commonlibrary.compat.SlcSystemUtils;
import android.slc.module.project.repository.local.ProjectConfig;
import cn.hutool.log.LogFactory;
import cn.hutool.log.dialect.console.ConsoleLogFactory;
import de.saxsys.mvvmfx.MvvmFX;
import org.jetbrains.annotations.NotNull;

import java.util.Locale;
import java.util.ResourceBundle;

public class TaskFirst extends Task {

    public TaskFirst() {
        super(TaskConstant.TASK_FIRST, false);

    }

    @Override
    protected void run(@NotNull String s) {
        LogFactory.setCurrentLogFactory(new ConsoleLogFactory());
        //fx
        ResourceBundle resourceBundle = ResourceBundle.getBundle("app", Locale.getDefault());
        SlcResourceUtils.tryAddResourceBundle(resourceBundle);
        MvvmFX.setGlobalResourceBundle(SlcResourceUtils.getDefResourceBundle());
        //config
        initAppConfig();
        ApiConfig.getInstance().setServiceApiAddress("http://localhost:8080/");
    }

    private void initAppConfig() {
        int appVersionCode;
        try {
            appVersionCode = Integer.parseInt(SlcResourceUtils.getString("app_version_code"));
        } catch (Exception e) {
            appVersionCode = 1;
        }
        String appVersionName;
        try {
            appVersionName = SlcResourceUtils.getString("app_version_name");
        } catch (Exception e) {
            appVersionName = "1.0";
        }
        boolean isRelease = SlcSystemUtils.runJarMode("assets/run-jar-perception");
        if (ProjectConfig.getInstance().isForcedDebug()) {
            isRelease = false;
        }
        AppConfig.init(appVersionCode, appVersionName, isRelease);
    }
}
